<?php
class Admin_Abstract
    extends Zend_Controller_Action
{
    protected $_layout;

    protected $_session = null;

    protected function _permision($name)
    {
        $_helper = new Admin_Permission_Resource();
        if(!$_helper->checkPermision($name)) {
            $this->_forward('denaied');
            return;
        }
    }

    protected function _getSession()
    {
        if(!isset($this->_session)) {
            $this->_session = new Model_Admin_Session();
        }
        return $this->_session;
    }

    public function preDispatch()
    {
        $this->createConfig();
        Zend_Registry::set('request', $this->getRequest());
        $this->setNoRenderer(true);
        if(!$this->_getSession()->isUserLoginedIn()) {
            if($this->getActionName()!='admin_login_index' && $this->getActionName()!='admin_login_logining') {
               $this->_redirect('admin/login/'); 
            }
        }
    }

    protected function createConfig()
    {
        $config = Bootstrap::getConfig();
        $design = $config->path->design->admin;
        $config = $config->toArray();
        $config['path']['design'] = $design;
        $config = new Zend_Config($config);
        Bootstrap::setStoreConfig($config);
    }

    protected function setNoRenderer($flag)
    {
        if(isset($flag)) {
            $this->_helper->viewRenderer->setNoRender($flag);
        }
    }

    public function loadLayout()
    {
        $this->_layout = new Layout_Abstract();
        $this->_layout->loadLayout($this->getActionName());
        return $this;
    }

    public function renderLayout()
    {
        $this->_layout->renderLayout();
        return $this;
    }

    public function getActionName()
    {
        $params = $this->getRequest()->getParams();
        return $params['module'].'_'.$params['controller'].'_'.$params['action'];
    }

    public function denaiedAction()
    {
        die($this->getActionName());
    }
}